'use strict';

var app = angular.module('app');
app.controller('authUpdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster','$modal',
    function($scope, $rootScope, $state, $timeout, toaster, $modal) {

        $scope.record = {
        	authType:$state.params.authorType,
        	id:$state.params.id,
        	status:1
        };
        if($state.includes('**.authm.cardAuth.update')){
        	$scope.loading = true;
            $scope.title="修改授权";
            $scope.reset = false;//修改不允许重置
            var id = $state.params.id;
            activate(id);
        }else if($state.includes('**.authm.cardAuth.create')){
            $scope.title="新增授权";
            $scope.reset = true;//新增时可以重置
        }
          validate();
      

        $scope.selUser = function(){
            $scope.record.personId=$scope.record.person.split('_')[0];
            $scope.record.cardNo=$scope.record.person.split('_')[1];
        }
		
		//重置表单
		$scope.resetFrom = function(){
		   $scope.record = {
	        	authType:$state.params.authorType,
	        	id:$state.params.id,
	        	status:1
        	};
		}
        // 初始化页面
        function activate(id) {
            $scope.loading = true;
            $.ajax({
                type: 'PUT',
                dataType: 'json',
                contentType:'application/json;charset=UTF-8',
                url : '/dac/authm/selectByAuthorId',
                data: angular.toJson({'id': id})
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    var item = result.data;
                    $scope.pname = item.personName;
                    $scope.record.personId = item.personId;
                    $scope.bname = item.locationName;
                    $scope.record.deviceId = item.deviceId;
                    $scope.record.authNo = item.authNo;
                    if($scope.record.authType!='1'){
                        $scope.record.authType = item.authType;
                    }
                    $scope.record.status = item.status;
                    $scope.record.endTime = item.endTime;
                } else {
                    $scope.msg = result.msg;
                }
                $scope.$apply();
            });
        }
        //表单的提交
        $scope.submit= function(){
            var m = $scope.record;
            if($scope.record.id != null){//id非空代表着是修改
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType:'application/json;charset=UTF-8',
                        url : 'dac/authm/cardAuth/update',
                        data: angular.toJson($scope.record)
                    }).then(function(result){
                    	if(result.httpCode ==200){//成功
	                        toaster.clear('*');
	                        toaster.pop('success', '', "保存成功");
	                        $timeout(function(){
	                            $state.go('main.authm.cardAuth.list');
	                        },2000);
	                    }else{
	                        toaster.clear('*');
	                        toaster.pop('error', '', result.msg);
	                        $scope.isDisabled = false;
	                    }
                    });
                }
            }else{//id为空代表着是新增
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType:'application/json;charset=UTF-8',
                        url : 'dac/authm/cardAuth/insert',
                        data: angular.toJson($scope.record)
                    }).then(function(result){
                        if(result.httpCode ==200){//成功
                        toaster.clear('*');
                        toaster.pop('success', '', "保存成功");
                        $timeout(function(){
                            $state.go('main.authm.cardAuth.list');
                        },2000);
                    }else{
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = false;
                    }
                    });
                }
               
            }

        }
        //表单验证
        function validate(userId){
            jQuery('form').validate({
                rules: {
                    pname: {
                        required: true
                    },
                    bname: {
                        required: true
                    },
                    authNo: {
                        required: true
                    },
                    authType: {
                        required: true
                    },
                    endTime: {
                        required: true
                    }
                },
                messages: {
                    pname: {
                        required: '请填写姓名'
                    },
                    bname: {
                        required: '请填写房屋地址'
                    },
                    authNo: {
                        required: '请填写门禁卡号'
                    },
                    authType: {
                        required: '请选择卡类型'
                    },
                    endTime: {
                        required: '请选择结束时间'
                    }
                },
                submitHandler: function() {
                    $scope.submit();
                }
            });
        }

        $scope.openDevice = function() {
            var modalInstance = $modal.open({
                templateUrl : 'dmDeviceList.html',//script标签中定义的id
                controller : 'deviceModalCtrl',//modal对应的Controller
                size: 'lg',
                resolve : {
                    items : function() {
                        return $scope.items;
                    }
                }
            });

            modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数

            });
            modalInstance.result.then(function(result) {

                $scope.bname=result[0];
                $scope.record['deviceId']=result[1];
            }, function(reason) {
                console.log(reason);
            });

        };

        $scope.openPerson = function() {
            var modalInstance = $modal.open({
                templateUrl : 'personList.html',//script标签中定义的id
                controller : 'personModalCtrl',//modal对应的Controller
                size: 'lg',
                resolve : {
                    items : function() {
                        return $scope.items;
                    }
                }
            });

            modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数

            });
            modalInstance.result.then(function(result) {

                $scope.pname=result[0];
                $scope.record['personId']=result[1];
                $scope.record.authNo = result[2];
            }, function(reason) {
                console.log(reason);
            });

        };

        //日期控件
        $scope.dateOptions = {
            startingDay: 1,
            class: 'datepicker'
        };
        //日期控件
        $scope.format = "yyyy-MM-dd";
        $scope.openDatepicker = function($event, i) {
            $event.preventDefault();
            $event.stopPropagation();
            if(i==1){
                $scope.beginTime = true;
            }else if(i==2){
                $scope.endTime = true;
            }
        }


    }]);



app.controller('deviceModalCtrl', function($scope, $rootScope, $modal,$modalInstance,items) {
    $scope.param = { };
    $scope.search = function () {
        $scope.loading = true;
        if($scope.pageFlag != 1){//未点击翻页
            $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
        }else{//点击翻页
            $scope.pageFlag = 2;//将开关值设置为2
        }
        $.ajax({
            type: 'POST',
            dataType: 'json',
            contentType:'application/json;charset=UTF-8',
            url : '/dac/dm/dmManager/list',
            data: angular.toJson($scope.param)
        }).then(function(result) {
            $scope.loading = false;
            if (result.httpCode == 200) {
                $scope.pageInfo = result;
            } else {
                $scope.msg = result.msg;
            }
            $scope.$apply();
        });
    }

    $scope.search();

    $scope.select = function(item) {
        $scope.params = [];
        $scope.params[0]=item.address;
        $scope.params[1]=item.deviceId;
        $modalInstance.close($scope.params);
    };
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };

    // 翻页
    $scope.pagination = function (page) {
        $scope.param.pageNum=page;
        $scope.pageFlag = 1;//将开关值设置为1
        $scope.search();
    };
});

app.controller('personModalCtrl', function($scope, $rootScope, $modal,$modalInstance,items) {

    $scope.paramP = { };

    $scope.searchP = function () {
        console.log( $scope.paramP)
        $scope.loading = true;
        if($scope.pageFlag != 1){//未点击翻页
            $scope.paramP.pageNum=1;//将页码设置为1，即跳转回第一页
        }else{//点击翻页
            $scope.pageFlag = 2;//将开关值设置为2
        }
        $.ajax({
            type: 'PUT',
            dataType: 'json',
            contentType:'application/json;charset=UTF-8',
            url : '/dac/userManager/list',
            data: angular.toJson($scope.paramP)
        }).then(function(result) {
            $scope.loading = false;
            if (result.httpCode == 200) {
                $scope.pageInfo = result;
            } else {
                $scope.msg = result.msg;
            }
            $scope.$apply();
        });
    }

    $scope.searchP();

    $scope.selectPerson = function(pid,pname,IDCardNo) {
        $scope.params = [];
        $scope.params[0]=pname;
        $scope.params[1]=pid;
        $scope.params[2]=IDCardNo;
        $modalInstance.close($scope.params);
    };
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };

    // 翻页
    $scope.pagination = function (page) {
        $scope.paramP.pageNum=page;
        $scope.pageFlag = 1;//将开关值设置为1
        $scope.searchP();
    };
});